package com.copa.ums.modules.ums.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.copa.ums.common.api.CommonPage;
import com.copa.ums.common.vo.ResponseVo;
import com.copa.ums.modules.ums.model.UmsResource;
import com.copa.ums.modules.ums.mapper.UmsResourceMapper;
import com.copa.ums.modules.ums.service.UmsResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copa.ums.modules.ums.service.UmsUserCacheService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author copa
 * @since 2022-10-31
 */
@Service
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements UmsResourceService {

    @Resource
    private UmsResourceMapper resourceMapper;

    @Resource
    private UmsUserCacheService userCacheService;

    @Override
    public List<UmsResource> getResourceList(Long userId) {
        return resourceMapper.getResourceList(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> create(UmsResource resource) {
        resource.setId(null);
        return save(resource)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> update(Long resourceId, UmsResource resource) {
        resource.setId(resourceId);
        userCacheService.delResourceListByResourceId(resourceId);
        return updateById(resource)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> delete(Long resourceId) {
        userCacheService.delResourceListByResourceId(resourceId);
        return removeById(resourceId)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @Override
    public ResponseVo<CommonPage<UmsResource>> list(Long categoryId,
                                                    String nameKeyword,
                                                    String urlKeyword,
                                                    Integer pageSize,
                                                    Integer pageNum) {
        Page<UmsResource> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UmsResource> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UmsResource> lambda = wrapper.lambda();
        if (Objects.nonNull(categoryId)) {
            lambda.eq(UmsResource::getCategoryId, categoryId);
        }
        if (StrUtil.isNotEmpty(nameKeyword)) {
            lambda.like(UmsResource::getName, nameKeyword);
        }
        if (StrUtil.isNotEmpty(urlKeyword)) {
            lambda.like(UmsResource::getUrl, urlKeyword);
        }
        return ResponseVo.success(CommonPage.restPage(page(page,wrapper)));
    }

    @Override
    public List<UmsResource> getResourceListByRoleId(Long roleId) {
        return resourceMapper.getResourceListByRoleId(roleId);
    }
}
